package vnc.server.rfb.messages;
import java.awt.AWTException;
import java.awt.Event;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.net.Socket;



public class KeyEventMessage extends Message {
	
	private final Robot robot;
	
	public KeyEventMessage(Socket clientSocket) throws AWTException
	{
		super(clientSocket, ProcessMode.read);
		this.robot = new Robot();
	}
	
	@Override
	public Object doReadMessage() throws IOException {

		System.err.println("key");
		byte downflag = din.readByte();
		din.skipBytes(2);
		int rfbkeyCode = din.readInt();
		
		int awtKeyCode = convertToAwtKeyCode(rfbkeyCode);
		if( downflag != 0 ){
			robot.keyPress(awtKeyCode);
		} else {
			robot.keyRelease(awtKeyCode);
		}
		return null;
	}

	private int convertToAwtKeyCode(int rfbkeyCode) {
	final int awtkey;
		
		if(rfbkeyCode >= KeyEvent.VK_0 && rfbkeyCode <= KeyEvent.VK_9){
			awtkey = rfbkeyCode ;
			System.out.println("key event " + rfbkeyCode + " " + awtkey);
			return awtkey;
		} else 	if(rfbkeyCode >= 0x61 && rfbkeyCode <= 0x7a){
			awtkey = rfbkeyCode - 32;
			System.out.println("key event " + rfbkeyCode + " " + awtkey);
			return awtkey;
		}
		
		 switch (rfbkeyCode) {
	      case Keysyms.Home :        awtkey =  KeyEvent.VK_HOME; break;
	      case Keysyms.End:          awtkey =  KeyEvent.VK_END; break;
	      case Keysyms.Page_Up:      awtkey =  KeyEvent.VK_PAGE_UP; break;
	      case Keysyms.Page_Down:    awtkey =  KeyEvent.VK_PAGE_DOWN; break;
	      case Keysyms.Up:           awtkey =  KeyEvent.VK_UP;  break;
	      case Keysyms.Down:         awtkey =  KeyEvent.VK_DOWN; break;
	      case Keysyms.Left:         awtkey =  KeyEvent.VK_LEFT; break;
	      case Keysyms.Right:        awtkey =  KeyEvent.VK_RIGHT; break;
	      case Keysyms.F1:           awtkey =  KeyEvent.VK_F1; break;
	      case Keysyms.F2:           awtkey =  KeyEvent.VK_F2; break;
	      case Keysyms.F3:           awtkey =  KeyEvent.VK_F3; break;
	      case Keysyms.F4:           awtkey =  KeyEvent.VK_F4; break;
	      case Keysyms.F5:           awtkey =  KeyEvent.VK_F5; break;
	      case Keysyms.F6:           awtkey =  KeyEvent.VK_F6; break;
	      case Keysyms.F7:           awtkey =  KeyEvent.VK_F7; break;
	      case Keysyms.F8:           awtkey =  KeyEvent.VK_F8; break;
	      case Keysyms.F9:           awtkey =  KeyEvent.VK_F9; break;
	      case Keysyms.F10:          awtkey =  KeyEvent.VK_F10; break;
	      case Keysyms.F11:          awtkey =  KeyEvent.VK_F11; break;
	      case Keysyms.F12:          awtkey =  KeyEvent.VK_F12; break;
	      case Keysyms.Print:        awtkey =  KeyEvent.VK_PRINTSCREEN; break;
	      case Keysyms.Pause:        awtkey =  KeyEvent.VK_PAUSE; break;
	      case Keysyms.Insert:       awtkey =  KeyEvent.VK_INSERT; break;
	      case Keysyms.Delete:       awtkey =  KeyEvent.VK_DELETE; break;
	      case Keysyms.BackSpace:    awtkey =  KeyEvent.VK_BACK_SPACE; break;
	      case Keysyms.Tab:          awtkey =  KeyEvent.VK_TAB; break;
	      case Keysyms.Return:       awtkey =  KeyEvent.VK_ENTER; break;
	      case Keysyms.Escape:       awtkey =  KeyEvent.VK_ESCAPE; break;
	      
	      case Keysyms.Shift_L:       awtkey =  KeyEvent.VK_SHIFT; break;
	      case Keysyms.Shift_R:       awtkey =  KeyEvent.VK_SHIFT; break;
	      case Keysyms.Control_L:       awtkey =  KeyEvent.VK_CONTROL; break;
	      case Keysyms.Control_R:       awtkey =  KeyEvent.VK_CONTROL; break;
	      case Keysyms.Alt_L:       awtkey =  KeyEvent.VK_ALT; break;
	      case Keysyms.Alt_R:       awtkey =  KeyEvent.VK_ALT; break;
	      default: awtkey = KeyEvent.VK_ESCAPE;
	      }
		 
		 System.out.println("key event " + rfbkeyCode + " " + awtkey);
		return awtkey;
	}
}
